package com.redstar.expenses;

import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;


public class QuickAdd extends SherlockActivity implements LocationListener{
	
	private DatabaseHelper db;
	private Expense exp;
	private GeoPoint point;
	private LocationManager lm;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
        
        exp = new Expense(Calendar.getInstance(),this);
        //get location
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L, 500.0f, this); 

        db = new DatabaseHelper(this);

        Toast.makeText(getApplicationContext(), "Expense Added", Toast.LENGTH_SHORT).show();
        finish();       
        
        
        
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null) {
			double lat = location.getLatitude();
			double lng = location.getLongitude();
			point = new GeoPoint((int) (lat * 1000000), (int) (lng * 1000000));
			//update expense
			exp.setLat((int) (lat * 1000000));
			exp.setLong((int) (lng * 1000000));
	        db.addExpense(exp);

			}				
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
}